package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.model.service.*;
import com.atguigu.lease.web.admin.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.admin.service.impl.ApartmentInfoServiceImpl;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    ApartmentInfoService apartmentInfoService;

    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    GraphInfoService graphInfoService;

    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }



    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        IPage<ApartmentItemVo> iPage =new Page<>(current,size);
        IPage<ApartmentItemVo> list = apartmentInfoService.pageAppartmentItemByQuery(iPage,queryVo);
        return Result.ok(list);
    }

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        ApartmentDetailVo apparmentDetailById = apartmentInfoService.getApparmentDetailById(id);
        return Result.ok(apparmentDetailById);
    }

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        //根据id删除info
       /* LambdaQueryWrapper<ApartmentInfo> li = new LambdaQueryWrapper<>();
        li.eq(ApartmentInfo::getId,id);*/
        apartmentInfoService.removeById(id);

        //根据id删除配套
        LambdaQueryWrapper<ApartmentFacility> lf = new LambdaQueryWrapper<>();
        lf.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(lf);


        //根据公寓Id删除图片
        LambdaQueryWrapper<GraphInfo> lg = new LambdaQueryWrapper<>();
        lg.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        lg.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(lg);


        //根据公寓ID删除 杂费
        LambdaQueryWrapper<ApartmentFeeValue> lfv = new LambdaQueryWrapper<>();
        lfv.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(lfv);

        return Result.ok();
    }

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> lambdaUpdateWrapper =new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApartmentInfo::getId,id);
        lambdaUpdateWrapper.set(ApartmentInfo::getIsRelease,status);
        apartmentInfoService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        LambdaQueryWrapper<ApartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(id!=null,ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = apartmentInfoService.list(lambdaQueryWrapper);
        return Result.ok(list);
    }
}














